﻿using RTW.Core.Datas;
using RTW.Core.Ioc;
using RTW.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace RTW.Servers
{
    public class DepartmentServer : BaseDAL<TDepartment>, IAutoInject
    {
        public async Task<IList<TDepartment>> GetAllAsync()
        {
            return await DbType.DB().Select<TDepartment>().ToListAsync<TDepartment>();
        }
        public async Task<TDepartment> GetByOneAsync(string ID)
        {
            return await DbType.DB().Queryable<TDepartment>().Include(m=>m.PDM).IncludeMany(m => m.DMS).Where(m => m.ID == ID).ToOneAsync();
        }

        public IList<TDepartment> GetAll()
        {
            return DbType.DB().Select<TDepartment>().ToList();
        }

        //逻辑删除
        public bool LogicalDelete(string ID)
        {
            var result = DbType.DB().Update<TDepartment>().Set(a => a.IsDelete, "1").Where(m => m.ID == ID).ExecuteAffrows();
            return result > 0;
        }



        //获取部门角色列表
        public List<TDepartment> GetDpetRoleList()
        {
            var list = DbType.DB().Queryable<TDepartment>()
                        .IncludeMany(a => a.Roles)//贪婪加载角色
                        .Where(m => m.IsDelete != "1")
                        .OrderBy("DMLevel asc,RowIndex asc")
                        .ToList();
            return list;
        }

        public async Task<List<TDepartment>> GetDpetTreeList()
        {
            var list = DbType.DB().Queryable<TDepartment>()
            .Where(m => m.IsDelete != "1")
            .Where(m=>string.IsNullOrEmpty(m.ParentID))
            .AsTreeCte()
            .OrderBy(m => m.DMLevel)
            .OrderBy(m => m.RowIndex)
            .ToTreeListAsync();
            return await list;
        }
    }
}
